/**
 * 作者:hanbo
 * 日期:2018/12/511:16
 * 描述:矢量图层
 */
import {Layer} from "./Layer";
import {BoundingBox} from "../geometry/BoundingBox";
import {PointStyle} from "../geometry/style/PointStyle";
import {LineStyle} from "../geometry/style/LineStyle";
import {PolygonStyle} from "../geometry/style/PolygonStyle";

export class GraphicLayer extends Layer {
    constructor() {
        super();
        this.geometrys = [];
    }

    /**
     * 向矢量图层里添加几何图形,默认图层类型为第一个添加的几何的类型
     * @param geometry {Geometry} 几何体
     */
    addGeometry(geometry) {
        if (!this.type) {
            this.type = geometry.type;
            this.geometrys.push(geometry);
            this.box = geometry.box;
        } else if (this.type === geometry.type) {
            this.geometrys.push(geometry);
            this.box.addBox(geometry.box)
        } else {
            console.error("类型不一致")
        }
    }

    /**
     * 根据地图范围绘制
     * @param mapRender {MapRender} 渲染器
     * @param extend {Rectangle} 坐标范围
     * @param level {Number} 地图等级
     * @param layerAmount {number} 图层数量
     * @param layerIndex {number} 图层索引
     * @override
     */
    draw(mapRender, extend, level,layerAmount,layerIndex) {
        const box = BoundingBox.translate(extend);
        if (!(BoundingBox.isIntersect(box, this.box) && this.type)) {
            return
        }
        let result = this._getGeoInExtend(box);//计算范围内的几何图形

        switch (this.type) {
            case "point":
                mapRender.drawPoints(result, this.uuid,layerAmount,layerIndex, this.style);
                break;
            case "rectangle":
                mapRender.drawRectangles(result, this.uuid,layerAmount,layerIndex, this.style);
                break;
            case "circle":
                break;
            case "line":
                mapRender.drawLines(result, this.uuid, layerAmount,layerIndex,this.style);
                break;
            case "multipoint":
                break;
            case "polygon":
                mapRender.drawPolygons(result, this.uuid,layerAmount,layerIndex, this.style);
                break;
            case "polyline":
                mapRender.drawPolyLines(result, this.uuid,layerAmount,layerIndex, this.style);
                break;
            default:
                break;
        }

    }
    /**
     * 清空图层的几何体
     */
    clearGeometry() {
        this.geometrys = [];
    }

    /**
     * 获取在矩形坐标范围内或与矩形相交的几何体
     * @param extend {BoundingBox} 坐标范围
     * @returns {Array} 集合体数组
     * @private
     */
    _getGeoInExtend(extend) {
        if (BoundingBox.boxInExtend(this.box, extend)) {
            return this.geometrys;
        }
        let result = [];
        let geometry;
        for (let i = 0; i < this.geometrys.length; i++) {
            geometry = this.geometrys[i];
            if (BoundingBox.isIntersect(extend, geometry.box)) {
                result.push(geometry)
            }
        }
        return result
    }

    /**
     * 设置图层样式
     * @param style {Style} 样式
     */
    setStyle(style) {
        if (this.type) {
            switch (this.type) {
                case "point":
                case "multipoint":
                    if (PointStyle.prototype.isPrototypeOf(style)) {
                        this.style = style;
                    }
                    else {
                        console.error("样式错误")
                    }
                    break;
                case "line":
                case "polyline":
                    if (LineStyle.prototype.isPrototypeOf(style)) {
                        this.style = style;
                    }
                    else {
                        console.error("样式错误")
                    }
                    break;
                case "rectangle":
                case "circle":
                case "polygon":
                    if (PolygonStyle.prototype.isPrototypeOf(style)) {
                        this.style = style;
                    }
                    else {
                        console.error("样式错误")
                    }
                    break;
                default:
                    break;
            }
        } else {
            console.error("请线添加几何要素，然后设置样式")
        }

    }

}